

import javax.swing.*;
import java.math.*;
import java.text.MessageFormat;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; 


class BiocalcPanel extends JPanel implements ActionListener
{
	private JButton confirmButton, 
					nextButton,
					exitButton,
					printButton;
	private JLabel labelChemicalNumber,
					labelVolume,
					labelCount,
					labelChemicalName;
	private JLabel labelMolecularWeight, labelConcentration, labelAuthor;
	private JTextField textFieldChemicalNumber,
						textFieldVolume,
						textFieldChemicalName,
						textFieldMolecularWeight,
						
						textFieldConcentration;
	private JTextArea reviewTextArea,
						outPutTextArea,
						combinedTextArea;
	
	double volume = 0.0;
	int i = 0;
	Chemical [] c = new Chemical [100];
	
	int number = 0;
	
	
	public BiocalcPanel ()
	{
		setLayout (new GridBagLayout());
			
		
		// the first line
		JPanel ChemicalNumberPanel = new JPanel ();
		ChemicalNumberPanel.setLayout(new FlowLayout (FlowLayout.LEFT));
			
		labelChemicalNumber = new JLabel ("Chemical numbers to be calculated in a solution");
		textFieldChemicalNumber = new JTextField (10);
		ChemicalNumberPanel.add(labelChemicalNumber);
		ChemicalNumberPanel.add(textFieldChemicalNumber);
		add (ChemicalNumberPanel, getConstraints (0, 0, 2, 1, GridBagConstraints.WEST));
		
			
		
		labelVolume = new JLabel ("Volume needed (ml)");
		textFieldVolume = new JTextField (10);
		JPanel volumePanel = new JPanel ();
		volumePanel.setLayout(new FlowLayout (FlowLayout.LEFT));
		volumePanel.add (labelVolume);
		volumePanel.add (textFieldVolume);
		add (volumePanel, getConstraints (0, 1, 1, 1, GridBagConstraints.WEST));
		
		
		confirmButton = new JButton ("Confirm");
		confirmButton.addActionListener (this);
		add (confirmButton, getConstraints (1, 1, 1, 1, GridBagConstraints.EAST));
		
			
		
		labelCount = new JLabel ("Chemical No.1 ");
		add (labelCount, getConstraints (0, 2, 1, 1, GridBagConstraints.WEST));
		
		labelChemicalName = new JLabel ("Chemical Name");
		textFieldChemicalName = new JTextField (10);
		textFieldChemicalName.setEditable(false);
		add (labelChemicalName, getConstraints (0, 3, 1, 1, GridBagConstraints.EAST));
		add (textFieldChemicalName, getConstraints (1, 3, 1, 1, GridBagConstraints.WEST));
		
		
		labelMolecularWeight = new JLabel ("Molecular Weight");
		textFieldMolecularWeight = new JTextField (10);
		textFieldMolecularWeight.setEditable(false);
		add (labelMolecularWeight, getConstraints (0, 4, 1, 1, GridBagConstraints.EAST));
		add (textFieldMolecularWeight, getConstraints (1, 4, 1, 1, GridBagConstraints.WEST));
		
		
		labelConcentration = new JLabel ("Concentration (M)");
		textFieldConcentration = new JTextField (10);
		textFieldConcentration.setEditable(false);
		add (labelConcentration, getConstraints (0, 5, 1, 1, GridBagConstraints.EAST));
		add (textFieldConcentration, getConstraints (1, 5, 1, 1, GridBagConstraints.WEST));
		
		nextButton = new JButton ("Next");
		nextButton.addActionListener (this);
		nextButton.setEnabled(false);
		add (nextButton, getConstraints (1, 6, 1, 1, GridBagConstraints.EAST));
		
		reviewTextArea = new JTextArea ("Copy of your input", 7, 40);
		reviewTextArea.setLineWrap (true);
		reviewTextArea.setWrapStyleWord (true);
		reviewTextArea.setEditable(false);
		JScrollPane reviewScroll = new JScrollPane (reviewTextArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		add (reviewScroll);
		add (reviewScroll, getConstraints (0, 7, 2, 5, GridBagConstraints.WEST));
		
		
		outPutTextArea = new JTextArea ("Calculation result ", 7, 40);
		outPutTextArea.setLineWrap (true);
		outPutTextArea.setWrapStyleWord (true);
		outPutTextArea.setEditable(false);
		JScrollPane outputScroll = new JScrollPane (outPutTextArea, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		add (outputScroll);	
		add (outputScroll, getConstraints (0, 12, 2, 5, GridBagConstraints.WEST));
		
		printButton = new JButton("Print");
		exitButton = new JButton ("Exit");
		printButton.addActionListener (this);
		printButton.setEnabled (false);
		exitButton.addActionListener (this);
		exitButton.setEnabled (true);
		JPanel ButtonPanel = new JPanel ();
		ButtonPanel.setLayout(new FlowLayout (FlowLayout.LEFT));
		ButtonPanel.add (printButton);
		ButtonPanel.add (exitButton);
		add (ButtonPanel, getConstraints (1, 18, 1, 1, GridBagConstraints.EAST));
		
		labelAuthor = new JLabel ("Developed by Wenlei Cao");
		add (labelAuthor, getConstraints (0, 18, 1, 1, GridBagConstraints.EAST));
		
		}
		
	
	

	private GridBagConstraints getConstraints (int gridx, int gridy, int gridwidth, int gridheight, int anchor) 
	{
		GridBagConstraints c = new GridBagConstraints ();
		c.insets = new Insets (5, 5,5, 5 );
		c.ipadx = 0;
		c.ipady = 0;
		c.gridx = gridx;
		c.gridy = gridy;
		c.gridwidth = gridwidth;
		c.gridheight = gridheight;
		c.anchor = anchor;
		return c;
	}
	
	public boolean isValidData1 ()
	{
		   return SwingValidator.isPresent(textFieldChemicalNumber, "Number of chemicals to be calculated" )
			&& SwingValidator.isInteger(textFieldChemicalNumber, "Number of chemicals to be calculated" )
		    && SwingValidator.isPresent(textFieldVolume, "Number of chemicals to be calculated" )
		    && SwingValidator.isDouble(textFieldVolume, "Volume of solution" );
		    
	}
	
	public boolean isValidData2 ()
	{
		   return SwingValidator.isPresent(textFieldMolecularWeight, "Molecular weight" )
		    && SwingValidator.isDouble(textFieldMolecularWeight, "Molecular weight" )
		    && SwingValidator.isPresent(textFieldConcentration, "Chemical concentration" )
		    && SwingValidator.isDouble(textFieldConcentration, "Chemical concentration")
		    && SwingValidator.isPresent(textFieldChemicalName, "Chemical Name" );
	}
	
	
	
	
		public void actionPerformed (ActionEvent e)
		{
			Object source = e.getSource ();
			
			String chemicalName = "";
			double molecularWeight = 0.0;
			double chemicalConc = 0.0;
			
			if (source == exitButton)
			{
				System.exit(0);
			}
			
			else if (source == confirmButton)
				{
				if (isValidData1())
					{
					number = Integer.parseInt (textFieldChemicalNumber.getText());
					volume = Double.parseDouble (textFieldVolume.getText());
					reviewTextArea.setText("Total solution volume " + textFieldVolume.getText() + "ml\n");
					reviewTextArea.append("Name \t\t" + "MW\t" + "(M)\t" + "gram\n" );
					nextButton.setEnabled(true);
					textFieldMolecularWeight.setEditable(true);
					textFieldConcentration.setEditable(true);
					textFieldChemicalName.setEditable(true);
					confirmButton.setEnabled(false);
					}
				}
			else if (source == nextButton)
				{
					if (isValidData2())
					{
						chemicalName = textFieldChemicalName.getText();
						molecularWeight= Double.parseDouble (textFieldMolecularWeight.getText());
						chemicalConc = Double.parseDouble (textFieldConcentration.getText());
						
								
						
						if (i < number && i+1 != number)
						{
							labelCount.setText ("Chemical No. " + (i+2));
							c [i] = new Chemical (chemicalName, chemicalConc, volume, molecularWeight);
							reviewTextArea.append(c [i].getChemicalName(c[i])+ "\t\t" +  c[i].getMolecularWeight(c[i])+ "\t" + c[i].getChemicalConc (c[i])+ "\t" + c[i].getDecimalAmount(c[i])+ "\n");
							i++;
							textFieldMolecularWeight.setText("");
							textFieldConcentration.setText("");
							textFieldChemicalName.setText("");
							
							
						}
						else if (i < number && i+1 == number)
						{
							nextButton.setEnabled(false);
							c [i] = new Chemical (chemicalName, chemicalConc, volume, molecularWeight);
							reviewTextArea.append(c [i].getChemicalName(c[i])+ "\t\t" +  c[i].getMolecularWeight(c[i])+ "\t" + c[i].getChemicalConc (c[i])+ "\t" + c[i].getDecimalAmount(c[i])+ "\n");
							outPutTextArea.setText ("Procedure to make this solution: \n" + "Weight " );
							printButton.setEnabled(true);
							
							for (int j=0; j< number; j++)
							{
							outPutTextArea.append (c [j].getChemicalName (c[j]) + " " + c[j].getDecimalAmount(c[j])+ " " + "gram, ");
							}
							
							BigDecimal intVolume = new BigDecimal (Double.toString(volume*0.9));
							intVolume = intVolume.setScale (0, RoundingMode.HALF_UP);
							outPutTextArea.append ("dissovle in " + intVolume + " ml water " + "\n" +
						"pH the solution if needed, then add water to " + volume + "ml" );
						}
					}	
						
			
				}
			else if (source == printButton)
			{ 
				final MessageFormat header = new MessageFormat("Solution Maker written by Wenlei Cao");
				final MessageFormat footer = new MessageFormat("Page - {0}");
			

				SwingUtilities.invokeLater(new Runnable() {
		            public void run() {
		                try {
		                    combinedTextArea = new JTextArea (reviewTextArea.getText()+"\n\n" + outPutTextArea.getText());
		                    combinedTextArea.print(header, footer, true, null,
		                            null, true);
		                } catch (Exception ex) {
		                    ex.printStackTrace();
		                }
		            }
		        });
			}
			
		}
	}

